package br.ufrpe.bsi.pvs.view.admin;

import java.awt.Color;
import java.awt.Component;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import org.jdom2.Document;

import br.ufrpe.bsi.pvs.Fachada;
import br.ufrpe.bsi.pvs.model.roupa.Estilo;
import br.ufrpe.bsi.pvs.model.roupa.Genero;
import br.ufrpe.bsi.pvs.model.roupa.Roupa;
import br.ufrpe.bsi.pvs.model.roupa.Tamanho;
import br.ufrpe.bsi.pvs.model.roupa.TipoCorporeo;
import br.ufrpe.bsi.pvs.model.roupa.TipoRoupa;
import br.ufrpe.bsi.pvs.view.PvsFrame;
import br.ufrpe.bsi.pvs.xml.transformer.RoupaXML;

public class TelaCadastroRoupa extends javax.swing.JPanel {

	public static TelaCadastroRoupa INSTANCE = new TelaCadastroRoupa();

	private TelaCadastroRoupa() {
		initComponents();
	}

	private void submit() {
		Roupa r = new Roupa();
		
		r.setDescricao(txaDescricao.getText());
		r.setEstilo(readEstilo());
		r.setGenero(readGenero());
		r.setTipoRoupa(readTipoRoupa());
		r.setQuantEstoque(readEstoque());
		r.setTamanho(readTamanho());

		Document doc = RoupaXML.parseAddRequest(r);
		Fachada.sendRequest(doc);
		PvsFrame.INSTANCE.menu();
		JOptionPane.showMessageDialog(null, "Roupa Cadasttrada");

	}

	private void clean() {
		spQuantidade.setValue(0);
		txaDescricao.setText("");
		cbEstilo.setSelectedIndex(0);
		cbGenero.setSelectedIndex(0);
		cbTipoRoupa.setSelectedIndex(0);
		cbTamanho.setSelectedIndex(0);
	}

	private Estilo readEstilo() {
		return Estilo.getEstilo((String) cbEstilo.getSelectedItem());
	}

	private Genero readGenero() {
		return Genero.getGenero(cbGenero.getSelectedIndex());
	}

	private TipoRoupa readTipoRoupa() {
		return TipoRoupa.getTipoRoupa((String) cbTipoRoupa.getSelectedItem());
	}

	private Tamanho readTamanho() {
		return Tamanho.getTamanho((String) cbTamanho.getSelectedItem());
	}

	private int readEstoque() {
		return (Integer) spQuantidade.getValue();
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lbBack = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        cbTipoRoupa = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDescricao = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        cbGenero = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cbEstilo = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        spQuantidade = new javax.swing.JSpinner();
        lbSubmit = new javax.swing.JLabel();
        lbClean = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        cbTamanho = new javax.swing.JComboBox();

        setBackground(java.awt.Color.white);

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setFont(new java.awt.Font("Gabriola", 0, 24)); // NOI18N
        jLabel1.setText("Cadastro de Roupas");
        jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 15, 5, 15));
        jLabel1.setOpaque(true);

        lbBack.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/back.png"))); // NOI18N
        lbBack.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbBackMouseClicked(evt);
            }
        });

        jPanel2.setOpaque(false);

        jPanel1.setOpaque(false);

        cbTipoRoupa.setBackground(new java.awt.Color(245, 245, 245));
        cbTipoRoupa.setFont(new java.awt.Font("Gabriola", 0, 18)); // NOI18N
        cbTipoRoupa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Camisa", "Blusa", "Calca", "Vestido", "Saia", "Short", "Bermuda" }));

        jLabel2.setFont(new java.awt.Font("Gabriola", 0, 24)); // NOI18N
        jLabel2.setText("Descricao");
        jLabel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 15, 5, 15));

        txaDescricao.setColumns(20);
        txaDescricao.setRows(5);
        jScrollPane1.setViewportView(txaDescricao);

        jLabel3.setFont(new java.awt.Font("Gabriola", 0, 24)); // NOI18N
        jLabel3.setText("Genero");
        jLabel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 15, 5, 15));

        cbGenero.setBackground(new java.awt.Color(245, 245, 245));
        cbGenero.setFont(new java.awt.Font("Gabriola", 0, 18)); // NOI18N
        cbGenero.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Masculino", "Feminino" }));

        jLabel4.setFont(new java.awt.Font("Gabriola", 0, 24)); // NOI18N
        jLabel4.setText("Estilo");
        jLabel4.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 15, 5, 15));

        cbEstilo.setBackground(new java.awt.Color(245, 245, 245));
        cbEstilo.setFont(new java.awt.Font("Gabriola", 0, 18)); // NOI18N
        cbEstilo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Casual", "Classico", "Fashion" }));

        jLabel5.setFont(new java.awt.Font("Gabriola", 0, 24)); // NOI18N
        jLabel5.setText("Tipo de Roupa");
        jLabel5.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 15, 5, 15));

        jLabel8.setFont(new java.awt.Font("Gabriola", 0, 24)); // NOI18N
        jLabel8.setText("Quantidade");
        jLabel8.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 15, 5, 15));

        spQuantidade.setFont(new java.awt.Font("Gabriola", 0, 18)); // NOI18N

        lbSubmit.setBackground(java.awt.Color.white);
        lbSubmit.setFont(new java.awt.Font("Gabriola", 0, 24)); // NOI18N
        lbSubmit.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbSubmit.setText("Salvar");
        lbSubmit.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(205, 205, 205)));
        lbSubmit.setOpaque(true);
        lbSubmit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbSubmitMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lbSubmitMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lbSubmitMouseExited(evt);
            }
        });

        lbClean.setBackground(java.awt.Color.white);
        lbClean.setFont(new java.awt.Font("Gabriola", 0, 24)); // NOI18N
        lbClean.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbClean.setText("Limpar");
        lbClean.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(205, 205, 205)));
        lbClean.setOpaque(true);
        lbClean.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbCleanMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lbCleanMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lbCleanMouseExited(evt);
            }
        });

        jLabel14.setFont(new java.awt.Font("Gabriola", 0, 24)); // NOI18N
        jLabel14.setText("Tamanho");
        jLabel14.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 15, 5, 15));

        cbTamanho.setBackground(new java.awt.Color(245, 245, 245));
        cbTamanho.setFont(new java.awt.Font("Gabriola", 0, 18)); // NOI18N
        cbTamanho.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PP", "P", "M", "G", "GG", "XG" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbTamanho, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(spQuantidade)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbTipoRoupa, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbGenero, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbEstilo, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(84, 84, 84)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbSubmit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lbClean, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbTamanho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(67, 67, 67)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(spQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbGenero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbEstilo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbTipoRoupa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbClean, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(lbSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cbEstilo, cbGenero, cbTipoRoupa, jLabel3});

        jPanel2.add(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 1176, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbBack, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lbBack, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(49, 49, 49))
        );
    }// </editor-fold>//GEN-END:initComponents

	private void lbBackMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_lbBackMouseClicked
		PvsFrame.INSTANCE.menu();
	}// GEN-LAST:event_lbBackMouseClicked

	private void lbCleanMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_lbCleanMouseClicked
		clean();
	}// GEN-LAST:event_lbCleanMouseClicked

	private void lbCleanMouseEntered(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_lbCleanMouseEntered
		lbClean.setBackground(new Color(245, 245, 245));
	}// GEN-LAST:event_lbCleanMouseEntered

	private void lbCleanMouseExited(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_lbCleanMouseExited
		lbClean.setBackground(Color.WHITE);
	}// GEN-LAST:event_lbCleanMouseExited

	private void lbSubmitMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_lbSubmitMouseClicked
		submit();
		clean();
	}// GEN-LAST:event_lbSubmitMouseClicked

	private void lbSubmitMouseEntered(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_lbSubmitMouseEntered
		lbSubmit.setBackground(new Color(245, 245, 245));
	}// GEN-LAST:event_lbSubmitMouseEntered

	private void lbSubmitMouseExited(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_lbSubmitMouseExited
		lbSubmit.setBackground(Color.WHITE);
	}// GEN-LAST:event_lbSubmitMouseExited

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbEstilo;
    private javax.swing.JComboBox cbGenero;
    private javax.swing.JComboBox cbTamanho;
    private javax.swing.JComboBox cbTipoRoupa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbBack;
    private javax.swing.JLabel lbClean;
    private javax.swing.JLabel lbSubmit;
    private javax.swing.JSpinner spQuantidade;
    private javax.swing.JTextArea txaDescricao;
    // End of variables declaration//GEN-END:variables
}
